package com.x.erp.controller;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


import org.apache.commons.lang.StringUtils;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.x.erp.model.Car;
import com.x.erp.model.SaleBill;
import com.x.erp.rpc.CarRpcService;
import com.x.erp.rpc.SaleBillRpcService;
import com.x.erp.util.ConstantState;
import com.x.framework.Base;
import com.x.framework.controller.BaseController;
import com.x.framework.controller.ResultCode;
import com.x.framework.controller.ResultModel;

/**
 * 统计信息
 */
@RestController
@RequestMapping("/statis")
public class StatisController extends BaseController{

	@Reference
	private SaleBillRpcService saleBillRpcService;//销售单据服务

	@Reference
	private CarRpcService carRpcService;//车辆服务

	/**
	 * 查询概要统计
	 * @param saleBill
	 * @param startDate 开始时间
	 * @param endDate 结束时间
	 * @return
	 */
	@RequestMapping(value = "/getOutline", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel getOutline(SaleBill saleBill, @DateTimeFormat(pattern="yyyy-MM-dd")Date startDate, @DateTimeFormat(pattern="yyyy-MM-dd")Date endDate) {
		Map<String, Object> res = new HashMap<>();
		//今日成交信息
		Map<String, Object> todaySaleInfo = saleBillRpcService.selectTodaySaleInfo(saleBill);
		//本月成交信息
		Map<String, Object> thisMonthSaleInfo = saleBillRpcService.selectThisMonthSaleInfo(saleBill);
		//累计成交信息
		Map<String, Object> totalSaleInfo = saleBillRpcService.selectTotalSaleInfo(saleBill);
		//条件查询成交信息
		Map<String, Object> saleInfo = saleBillRpcService.selectSaleInfoByCondition(saleBill, startDate, endDate);
		res.put("todaySaleInfo", todaySaleInfo);
		res.put("thisMonthSaleInfo", thisMonthSaleInfo);
		res.put("totalSaleInfo", totalSaleInfo);
		res.put("saleInfo", saleInfo);
		//如果市场ID为空，为总部查询
//		if(!Base.isNull(saleBill.getMarketId())) {
			//查询市场销售情况
		List<Map<String, Object>> marketSaleInfo = saleBillRpcService.selectMarketSaleInfo(saleBill, startDate, endDate);
		res.put("marketSaleInfo", marketSaleInfo);
//		}
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", res);
	}

	/**
	 * 查询入库概要统计
	 * @param car
	 * @param startDate 开始时间
	 * @param endDate 结束时间
	 * @return
	 */
	@RequestMapping(value = "/getCarStorageOutline", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel getCarStorageOutline(Car car, @DateTimeFormat(pattern="yyyy-MM-dd")Date startDate, @DateTimeFormat(pattern="yyyy-MM-dd")Date endDate) {
		if(StringUtils.isEmpty(car.getMarketId())){
			car.setMarketId("1");
		}
		Map<String, Object> res = new HashMap<>();
		Date today = this.getDate(Base.getCurrentDate(0, "yyyy-MM-dd"), "yyyy-MM-dd");//今天
		//今日入库量
		Map<String, Object> todayStorageInfo = carRpcService.selectCarStorageInfo(car, today, new Date());
		Date thisMonth = this.getDate(getCurrentDate(0, "yyyy-MM"), "yyyy-MM");//本月
		//本月入库量
		Map<String, Object> thisMonthStorageInfo = carRpcService.selectCarStorageInfo(car, thisMonth, new Date());
		//总入库量
		Map<String, Object> totalStorageInfo = carRpcService.selectCarStorageInfo(car, null, new Date());
		//市场入库信息
		List<Map<String, Object>> marketStorageInfo = carRpcService.selectMarketCarStorageInfo(car, startDate, endDate);
		//今日市场入库信息
		List<Map<String, Object>> todayMarketStorageInfo = carRpcService.selectMarketCarStorageInfo(car, today, new Date());
		res.put("todayStorageInfo", todayStorageInfo);
		res.put("thisMonthStorageInfo", thisMonthStorageInfo);
		res.put("totalStorageInfo", totalStorageInfo);
		res.put("marketStorageInfo", marketStorageInfo);
		res.put("todayMarketStorageInfo", todayMarketStorageInfo);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", res);
	}

	/**
	 * 查询车辆库存概要
	 * @param car
	 * @return
	 */
	@RequestMapping(value = "/app/getCarStorageOutline", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel getAppCarStorageOutline(Car car, @DateTimeFormat(pattern="yyyy-MM")Date month) {
		if(month == null) {
			return new ResultModel(ResultCode.RESULT_FAIL, "查询月份不能为空");
		}
		if(StringUtils.isEmpty(car.getMarketId())){
			car.setMarketId("1");
		}
		Map<String, Object> res = new HashMap<>();
		Calendar c = Calendar.getInstance();
		c.setTime(month);
		c.add(Calendar.MONTH, 1);
		//入库量
		Map<String, Object> totalStorageInfo = carRpcService.selectCarStorageInfo(car, month, c.getTime());
		//出库量
		Map<String, Object> totalOutStorageInfo = carRpcService.selectCarOutStockInfo(car, month, c.getTime());
		//本月库存量
		car.setCarStockState(2);
		Map<String, Object> thisMonthStorageInfo = carRpcService.selectCarStorageInfo(car, month, c.getTime());
		//当前在库
		Map<String, Object> currentStorageInfo = carRpcService.selectCarStorageInfo(car, null, null);
		res.put("totalStorageInfo", totalStorageInfo);
		res.put("totalOutStorageInfo", totalOutStorageInfo);
		res.put("thisMonthStorageInfo", thisMonthStorageInfo);
		res.put("currentStorageInfo", currentStorageInfo);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", res);
	}

	/**
	 * 日库存统计
	 * @param car
	 * @param startDate
	 * @return
	 */
	@RequestMapping(value = "/app/getCarStorageDay", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel getAppCarStorageDay(Car car, @DateTimeFormat(pattern="yyyy-MM-dd")Date startDate) {
		Map<String, Object> res = new HashMap<>();
		if(startDate == null){
			return new ResultModel(ResultCode.RESULT_FAIL, "查询日期不能为空");
		}
		if(StringUtils.isEmpty(car.getMarketId())){
			car.setMarketId("1");
		}
		Calendar c = Calendar.getInstance();
		c.setTime(startDate);
		c.add(Calendar.DATE, 1);
		//入库量
		Map<String, Object> storageInfo = carRpcService.selectCarStorageInfo(car, startDate, c.getTime());
		//出库量
		Map<String, Object> outStorageInfo = carRpcService.selectCarOutStockInfo(car, startDate, c.getTime());
		//预订量
		Map<String, Object> carOrderInfo = carRpcService.selectCarOrderInfo(car, startDate, c.getTime());
		res.put("storageInfo", storageInfo);
		res.put("outStorageInfo", outStorageInfo);
		res.put("carOrderInfo", carOrderInfo);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", res);
	}

	@RequestMapping(value = "/getRankInfo", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel getRankInfo(SaleBill saleBill, @DateTimeFormat(pattern="yyyy-MM-dd")Date startDate, @DateTimeFormat(pattern="yyyy-MM-dd")Date endDate) {
		Map<String, Object> res = new HashMap<>();
		//查询商铺成交量排行
		List<Map<String, Object>> shopSaleRankInfo = saleBillRpcService.selectShopSaleRank(saleBill, startDate, endDate);
		//查询销售额排行
		List<Map<String, Object>> salesVolumeRankInfo = saleBillRpcService.selectSalesVolumeRank(saleBill, startDate, endDate);
		//查询畅销品牌排行
		List<Map<String, Object>> brandRankInfo = saleBillRpcService.selectBrandRank(saleBill, startDate, endDate);
		//查询车型排行
		List<Map<String, Object>> modelRankInfo = saleBillRpcService.selectModelRank(saleBill, startDate, endDate);
		//查询畅销颜色排行
		List<Map<String, Object>> colorRankInfo = saleBillRpcService.selectColorRank(saleBill, startDate, endDate);
		//查询价格排行
		List<Map<String, Object>> priceRankInfo = saleBillRpcService.selectPriceRank(saleBill, startDate, endDate);
		res.put("shopSaleRankInfo", shopSaleRankInfo);
		res.put("salesVolumeRankInfo", salesVolumeRankInfo);
		res.put("brandRankInfo", brandRankInfo);
		res.put("modelRankInfo", modelRankInfo);
		res.put("colorRankInfo", colorRankInfo);
		res.put("priceRankInfo", priceRankInfo);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", res);
	}

	/**
	 * 查询概要统计
	 * @param saleBill
	 * @param startDate 开始时间
	 * @param endDate 结束时间
	 * @return
	 */
	@RequestMapping(value = "/app/getOutline", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel getAppOutline(SaleBill saleBill, @DateTimeFormat(pattern="yyyy-MM-dd")Date startDate, @DateTimeFormat(pattern="yyyy-MM-dd")Date endDate) {
		Map<String, Object> res = new HashMap<>();
		//累计成交信息
		Map<String, Object> totalSaleInfo = saleBillRpcService.selectTotalSaleInfo(saleBill);
		//查询昨日成交信息
		Date yesterday = this.getDate(Base.getCurrentDate(-1, "yyyy-MM-dd"), "yyyy-MM-dd");
		Date today = this.getDate(Base.getCurrentDate(0, "yyyy-MM-dd"), "yyyy-MM-dd");
		Map<String, Object> yesterdaySaleInfo = saleBillRpcService.selectSaleInfoByCondition(saleBill, yesterday, today);
		//查询畅销品牌排行
		List<Map<String, Object>> brandRankInfo = saleBillRpcService.selectBrandRank(saleBill, startDate, endDate);
		//查询车型排行
		List<Map<String, Object>> modelRankInfo = saleBillRpcService.selectModelRank(saleBill, startDate, endDate);
		//查询畅销颜色排行
		List<Map<String, Object>> colorRankInfo = saleBillRpcService.selectColorRank(saleBill, startDate, endDate);
		//查询价格排行
		List<Map<String, Object>> priceRankInfo = saleBillRpcService.selectPriceRank(saleBill, startDate, endDate);
		res.put("brandRankInfo", brandRankInfo);
		res.put("modelRankInfo", modelRankInfo);
		res.put("colorRankInfo", colorRankInfo);
		res.put("priceRankInfo", priceRankInfo);
		res.put("yesterdaySaleInfo", yesterdaySaleInfo);
		res.put("totalSaleInfo", totalSaleInfo);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", res);
	}

	/**
	 * 查询商铺成交量排行
	 * @param saleBill
	 * @param startDate 开始时间
	 * @param endDate 结束时间
	 * @return
	 */
	@RequestMapping(value = "/getShopSaleRank", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel getShopSaleRank(SaleBill saleBill, @DateTimeFormat(pattern="yyyy-MM-dd")Date startDate, @DateTimeFormat(pattern="yyyy-MM-dd")Date endDate) {
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", saleBillRpcService.selectShopSaleRank(saleBill, startDate, endDate));
	}

	/**
	 * 查询销售额排行
	 * @param saleBill
	 * @param startDate 开始时间
	 * @param endDate 结束时间
	 * @return
	 */
	@RequestMapping(value = "/getSalesVolumeRank", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel getSalesVolumeRank(SaleBill saleBill, @DateTimeFormat(pattern="yyyy-MM-dd")Date startDate, @DateTimeFormat(pattern="yyyy-MM-dd")Date endDate) {
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", saleBillRpcService.selectSalesVolumeRank(saleBill, startDate, endDate));
	}

	/**
	 * 查询畅销品牌排行
	 * @param saleBill
	 * @param startDate 开始时间
	 * @param endDate 结束时间
	 * @return
	 */
	@RequestMapping(value = "/getBrandRank", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel getBrandRank(SaleBill saleBill, @DateTimeFormat(pattern="yyyy-MM-dd")Date startDate, @DateTimeFormat(pattern="yyyy-MM-dd")Date endDate) {
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", saleBillRpcService.selectBrandRank(saleBill, startDate, endDate));
	}

	/**
	 * 查询车型排行
	 * @param saleBill
	 * @param startDate 开始时间
	 * @param endDate 结束时间
	 * @return
	 */
	@RequestMapping(value = "/getModelRank", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel getModelRank(SaleBill saleBill, @DateTimeFormat(pattern="yyyy-MM-dd")Date startDate, @DateTimeFormat(pattern="yyyy-MM-dd")Date endDate) {
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", saleBillRpcService.selectModelRank(saleBill, startDate, endDate));
	}

	/**
	 * 查询颜色排行
	 * @param saleBill
	 * @param startDate 开始时间
	 * @param endDate 结束时间
	 * @return
	 */
	@RequestMapping(value = "/getColorRank", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel getColorRank(SaleBill saleBill, @DateTimeFormat(pattern="yyyy-MM-dd")Date startDate, @DateTimeFormat(pattern="yyyy-MM-dd")Date endDate) {
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", saleBillRpcService.selectColorRank(saleBill, startDate, endDate));
	}

	/**
	 * 查询价格排行
	 * @param saleBill
	 * @param startDate 开始时间
	 * @param endDate 结束时间
	 * @return
	 */
	@RequestMapping(value = "/getPriceRank", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel getPriceRank(SaleBill saleBill, @DateTimeFormat(pattern="yyyy-MM-dd")Date startDate, @DateTimeFormat(pattern="yyyy-MM-dd")Date endDate) {
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", saleBillRpcService.selectPriceRank(saleBill, startDate, endDate));
	}

	/**
	 * 查询区域销售信息
	 * @param saleBill
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	@RequestMapping(value = "/getAreaSaleInfo", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel getAreaSaleInfo(SaleBill saleBill, @DateTimeFormat(pattern="yyyy-MM-dd")Date startDate, @DateTimeFormat(pattern="yyyy-MM-dd")Date endDate) {
		List<Map<String, Object>> res = new ArrayList<>();
		Map<String, List<Map<String, Object>>> resMap = new LinkedHashMap<>();
		List<Map<String, Object>> areaSaleInfo = saleBillRpcService.selectAreaSaleInfo(saleBill, startDate, endDate);
		if(areaSaleInfo != null) {
			//处理城市名称
			for(Map<String, Object> map : areaSaleInfo) {
				if(resMap.get(map.get("cityName")) == null) {
					List<Map<String, Object>> bs = new ArrayList<>();
					bs.add(map);
					resMap.put(map.get("cityName").toString(), bs);
				}else {
					resMap.get(map.get("cityName").toString()).add(map);
				}
			}
		}
		for(String key :resMap.keySet()) {
			Map<String, Object> map = new HashMap<>();
			map.put("cityName", key);
			map.put("areaSaleInfo", resMap.get(key));
			res.add(map);
		}
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", res);
	}

	/**
	 * 查询月销售信息
	 * @param saleBill
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	@RequestMapping(value = "/getMonthSaleInfo", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel getMonthSaleInfo(SaleBill saleBill, @DateTimeFormat(pattern="yyyy-MM")Date startDate, @DateTimeFormat(pattern="yyyy-MM")Date endDate) {
		Map<String, Object> res = new HashMap<>();
		int saleNum = 0;
		long saleVolume = 0;
		List<Map<String, Object>> monthSaleInfo = saleBillRpcService.selectMonthSaleInfo(saleBill, startDate, endDate);
		for(Map<String, Object> monthSaleInfoMap : monthSaleInfo) {
			//求和
			saleNum += Integer.valueOf(monthSaleInfoMap.get("saleNum").toString());
			saleVolume += Integer.valueOf(monthSaleInfoMap.get("saleVolume").toString());
		}
		res.put("saleNum", saleNum);
		res.put("saleVolume", saleVolume);
		res.put("monthSaleInfo", monthSaleInfo);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", res);
	}

	/**
	 * 查询库存品牌排行
	 * @param car
	 * @param month
	 * @param startDate
	 * @return
	 */
	@RequestMapping(value = "/app/getStorageBrandRank", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel getAppStorageBrandRank(Car car, @DateTimeFormat(pattern="yyyy-MM")Date month, @DateTimeFormat(pattern="yyyy-MM-dd")Date startDate, Integer statisType) {
		if(month == null && startDate ==null) {
			return new ResultModel(ResultCode.RESULT_FAIL, "查询日期不能为空！");
		}
		if(statisType == null) {
			return new ResultModel(ResultCode.RESULT_FAIL, "统计类型不能为空！");
		}
		Calendar c = Calendar.getInstance();
		if(month != null) {
			c.setTime(month);
			c.add(Calendar.MONTH, 1);
			startDate = month;
		}else {
			c.setTime(startDate);
			c.add(Calendar.DATE, 1);
		}
		if(car.getMarketId() == null){
			car.setMarketId("1");
		}
		switch (statisType) {
		case 1:
			//入库量
			return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", carRpcService.selectStorageBrandRank(car, startDate, c.getTime(), null));
		case 2:
			//出库量
			return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", carRpcService.selectCarOutStockBrandRank(car, startDate, c.getTime()));
		case 3:
			//本月库存量
			car.setCarStockState(ConstantState.CAR_STOCK_STATE_OUT_ING);
			return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", carRpcService.selectStorageBrandRank(car, startDate, c.getTime(), null));
		case 4:
			//当前在库
			car.setCarStockState(ConstantState.CAR_STOCK_STATE_OUT_ING);
			return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", carRpcService.selectStorageBrandRank(car, null, null, null));
		case 5:
			//预订
			return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", carRpcService.selectCarOrderBrandRank(car, startDate, c.getTime()));
		default:
			return new ResultModel(ResultCode.RESULT_FAIL, "统计类型错误！");
		}
	}

	/**
	 * 查询库存车系排行
	 * @param car
	 * @param month
	 * @param startDate
	 * @return
	 */
	@RequestMapping(value = "/app/getStorageSeriesRank", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel getAppStorageSeriesRank(Car car, @DateTimeFormat(pattern="yyyy-MM")Date month, @DateTimeFormat(pattern="yyyy-MM-dd")Date startDate, Integer statisType) {
		if(month == null && startDate == null) {
			return new ResultModel(ResultCode.RESULT_FAIL, "查询日期不能为空！");
		}
		if(statisType == null) {
			return new ResultModel(ResultCode.RESULT_FAIL, "统计类型不能为空！");
		}
		Calendar c = Calendar.getInstance();
		if(month != null) {
			c.setTime(month);
			c.add(Calendar.MONTH, 1);
			startDate = month;
		}else {
			c.setTime(startDate);
			c.add(Calendar.DATE, 1);
		}
		switch (statisType) {
		case 1:
			//入库量
			return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", carRpcService.selectStorageSeriesRank(car, startDate, c.getTime(), null));
		case 2:
			//出库量
			return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", carRpcService.selectCarOutStockSeriesRank(car, startDate, c.getTime()));
		case 3:
			//本月库存量
			car.setCarStockState(ConstantState.CAR_STOCK_STATE_OUT_ING);
			return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", carRpcService.selectStorageSeriesRank(car, startDate, c.getTime(), null));
		case 4:
			//当前在库
			car.setCarStockState(ConstantState.CAR_STOCK_STATE_OUT_ING);
			return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", carRpcService.selectStorageSeriesRank(car, null, null, null));
		case 5:
			//预订
			return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", carRpcService.selectCarOrderSeriesRank(car, startDate, c.getTime()));
		default:
			return new ResultModel(ResultCode.RESULT_FAIL, "统计类型错误！");
		}
	}

	/**
	 * 查询库存车龄排行
	 * @param car
	 * @param month
	 * @param startDate
	 * @return
	 */
	@RequestMapping(value = "/app/getStorageCarAgeRank", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel getAppStorageCarAgeRank(Car car, @DateTimeFormat(pattern="yyyy-MM")Date month, @DateTimeFormat(pattern="yyyy-MM-dd")Date startDate, Integer statisType) {
		if(month == null && startDate == null) {
			return new ResultModel(ResultCode.RESULT_FAIL, "查询日期不能为空！");
		}
		if(statisType == null) {
			return new ResultModel(ResultCode.RESULT_FAIL, "统计类型不能为空！");
		}
		Calendar c = Calendar.getInstance();
		if(month != null) {
			c.setTime(month);
			c.add(Calendar.MONTH, 1);
			startDate = month;
		}else {
			c.setTime(startDate);
			c.add(Calendar.DATE, 1);
		}
		switch (statisType) {
		case 1:
			//入库量
			return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", carRpcService.selectStorageCarAgeRank(car, startDate, c.getTime(), null));
		case 2:
			//出库量
			return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", carRpcService.selectCarOutStockCarAgeRank(car, startDate, c.getTime()));
		case 3:
			//本月库存量
			car.setCarStockState(ConstantState.CAR_STOCK_STATE_OUT_ING);
			return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", carRpcService.selectStorageCarAgeRank(car, startDate, c.getTime(), null));
		case 4:
			//当前在库
			car.setCarStockState(ConstantState.CAR_STOCK_STATE_OUT_ING);
			return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", carRpcService.selectStorageCarAgeRank(car, null, null, null));
		case 5:
			//预订
			return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", carRpcService.selectCarOrderCarAgeRank(car, startDate, c.getTime()));
		default:
			return new ResultModel(ResultCode.RESULT_FAIL, "统计类型错误！");
		}
	}

	/**
	 * 查询库存库龄排行
	 * @param car
	 * @param month
	 * @param startDate
	 * @return
	 */
	@RequestMapping(value = "/app/getStorageStockAgeRank", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel getAppStorageStockAgeRank(Car car, @DateTimeFormat(pattern="yyyy-MM")Date month, @DateTimeFormat(pattern="yyyy-MM-dd")Date startDate, Integer statisType) {
		if(month == null && startDate == null) {
			return new ResultModel(ResultCode.RESULT_FAIL, "查询日期不能为空！");
		}
		if(statisType == null) {
			return new ResultModel(ResultCode.RESULT_FAIL, "统计类型不能为空！");
		}
		Calendar c = Calendar.getInstance();
		if(month != null) {
			c.setTime(month);
			c.add(Calendar.MONTH, 1);
			startDate = month;
		}else {
			c.setTime(startDate);
			c.add(Calendar.DATE, 1);
		}
		switch (statisType) {
		case 1:
			//入库量
			return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", carRpcService.selectStorageStockAgeRank(car, startDate, c.getTime()));
		case 2:
			//出库量
			return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", carRpcService.selectCarOutStockStockAgeRank(car, startDate, c.getTime()));
		case 3:
			//本月库存量
			car.setCarStockState(ConstantState.CAR_STOCK_STATE_OUT_ING);
			return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", carRpcService.selectStorageStockAgeRank(car, startDate, c.getTime()));
		case 4:
			//当前在库
			car.setCarStockState(ConstantState.CAR_STOCK_STATE_OUT_ING);
			return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", carRpcService.selectStorageStockAgeRank(car, null, null));
		case 5:
			//预订
			return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", carRpcService.selectCarOrderStockAgeRank(car, startDate, c.getTime()));
		default:
			return new ResultModel(ResultCode.RESULT_FAIL, "统计类型错误！");
		}
	}

}
